﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace CollectArticle
{
    public class Configuration
    {
        private string path;
        public Configuration(string configPath)
        {
            path = configPath;
        }
       
        /// <summary>
        /// 网站属性
        /// </summary>
        public List<Site> Sites
        {
            get
            {
                return Query(path, "sites");
            }
        }
        #region 获取xml键值集合
        /// <summary>
        /// 获取xml键值集合
        /// configuration-->config
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>        
        public static List<Site> Query(string path, string elementsName)
        {
            var query = from q in XDocument.Load(path).Elements("configuration").Elements(elementsName).Elements("add")
                        select new Site
                        {
                            Uid = Convert.ToInt32(q.Attribute("uid").Value),
                            Type = Convert.ToInt32(q.Attribute("type").Value),
                            Encode = q.Attribute("encode").Value,
                            Url = q.Attribute("value").Value,
                            Ltype = q.Attribute("ltype").Value,
                            Lval = q.Attribute("lval").Value,
                            Dtype = q.Attribute("dtype").Value,
                            Dval = q.Attribute("dval").Value,
                            Tag = q.Attribute("tag").Value,
                            WebSite = q.Attribute("site").Value
                        };
           
            return query.ToList();
        }
        #endregion
    }

    public class Site
    {
        public int Uid { get; set; }

        public int Type { get; set; }

        public string Encode { get; set; }

        public string Url { get; set; }

        public string Ltype { get; set; }

        public string Lval { get; set; }

        public string Dtype { get; set; }

        public string Dval { get; set; }

        public string Tag { get; set; }

        public string WebSite { get; set; }
    }
}
